# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=setuptools
pkgbase=mingw-w64-python-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
_py3_base=3.6
_py2_base=2.7
pkgver=39.2.0
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
license=('PSF')
url="https://pypi.python.org/pypi/setuptools"
depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/setuptools/archive/v${pkgver}.tar.gz
        '0001-mingw-python-fix.patch'
        '0002-Allow-usr-bin-env-in-script.patch'
        '0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch'
        '0004-dont-execute-msvc.patch')
sha256sums=('ca8119dd5c2764a7d290518817de0b880d23d790913fcd797c02ad2aa39b8c41'
            'd3bc778723e63bbd6e43ab3536a015c547c8c20a95c6679a952284a7a4822996'
            '7bb5617c69566f5fbf1a3ee29a08179e1b41bdeabbc2998bf67615e9aa000424'
            '0e556505cb70ff3a5df856e352d5e2b3cf1582c25d02fc00a2d935a28576b28c'
            '2a854e21e99d6724999e2abeff08451923e9940b4092eb2b638702da17abdb73')

prepare() {
  cd "${srcdir}/setuptools-${pkgver}"
  patch -p1 -i ${srcdir}/0001-mingw-python-fix.patch
  patch -p1 -i ${srcdir}/0002-Allow-usr-bin-env-in-script.patch
  patch -p1 -i ${srcdir}/0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch
  if [ $(vercmp $2 6.9) -gt 0 ]; then
    patch -p1 -i ${srcdir}/0004-dont-execute-msvc.patch
  fi

  cd "${srcdir}"

  # Compile our own MSYS2-layout and /usr/bin/env capable {cli,gui}{-32,-64}.exe to replace the precompiled binaries.
  # .. when arm is ready, add it to this.
  PATH=/mingw32/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw32/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-64.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-64.exe setuptools-${pkgver}/launcher.c

  cp -r setuptools-${pkgver} setuptools-python2-${CARCH}
  cp -r setuptools-${pkgver} setuptools-python3-${CARCH}

  cd "${srcdir}"/setuptools-python2-${CARCH}
  sed -i -e "s|^#\!.*/usr/bin/python|#!/usr/bin/python2|" tests/manual_test.py
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python2|" setuptools/command/easy_install.py

  cd "${srcdir}"/setuptools-python3-${CARCH}
  sed -i -e "s|^#\!.*/usr/bin/python|#!/usr/bin/python3|" tests/manual_test.py
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python3|" setuptools/command/easy_install.py
}

build() {
  # Build python 2 module
  cd "${srcdir}"/setuptools-python2-${CARCH}
  ${MINGW_PREFIX}/bin/python2 bootstrap.py
  ${MINGW_PREFIX}/bin/python2 setup.py build

  # Build python 3 module
  cd "${srcdir}"/setuptools-python3-${CARCH}
  ${MINGW_PREFIX}/bin/python3 bootstrap.py
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package_python3-setuptools() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3>=3.3")

  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})

  cd "${srcdir}/setuptools-python3-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  # Move files conflicted with python2-setuptools
  mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe
  [[ -f ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe.manifest ]] && {
    mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe.manifest ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe.manifest
    sed -e "s|easy_install|easy_install-${_py3_base}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe.manifest
  }
  sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}-script.py
}

package_python2-setuptools() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2>=2.7")

  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})

  cd "${srcdir}/setuptools-python2-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py
  sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py2_base}-script.py
}

# Wrappers
package_mingw-w64-i686-python2-setuptools() {
  package_python2-setuptools
}

package_mingw-w64-i686-python3-setuptools() {
  package_python3-setuptools
}

package_mingw-w64-x86_64-python2-setuptools() {
  package_python2-setuptools
}

package_mingw-w64-x86_64-python3-setuptools() {
  package_python3-setuptools
}
